package gameMiniGames;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class MapGame extends JLayeredPane implements MouseListener{
	
	public ImageIcon[] pieces = new ImageIcon[17];
	public JLabel[] labels = new JLabel[17];
	public JLabel[] tempHold = new JLabel[2];
	public ImageIcon[] tempImage= new ImageIcon[2];
	boolean firstClick = true;
	public JLabel head = new JLabel(new ImageIcon("src/gameMiniGames/heading.png"));
	public JPanel jP = new JPanel();
	public String[][] board = new String[4][4];
	public String[][] game = new String[4][4];
	public boolean win = false;
	int[][] checking = new int[4][4];
	public boolean played = false;
	
	public MapGame(){
		for(int i = 0; i < 4; i++){
			for(int j = 0; j < 4; j++){
				board[i][j] = "src/MapGameImages/" + (j+1) + ".png";
				if(i == 1){
					board[i][j] = "src/MapGameImages/" + (j+5) + ".png";
				}
				if(i == 2){
					board[i][j] = "src/MapGameImages/" + (j+9) + ".png";
				}
				if(i == 3){
					board[i][j] = "src/MapGameImages/" + (j+13) + ".png";
				}
			}
		}
		setLayout(new BorderLayout());
		jP.setLayout(new GridLayout(4,4));
		for(int i = 1; i < 17; i++){
			pieces[i] = new ImageIcon("src/MapGameImages/" + i + ".png");
		}
		for(int i = 1; i < 17; i++){
			labels[i] = new JLabel(pieces[i]); 
			labels[i].addMouseListener(this);
		}

		jP.add(labels[1]);
		jP.add(labels[10]);
		jP.add(labels[6]);
		jP.add(labels[3]);
		jP.add(labels[11]);
		jP.add(labels[2]);
		jP.add(labels[12]);
		jP.add(labels[8]);
		jP.add(labels[4]);
		jP.add(labels[13]);
		jP.add(labels[9]);
		jP.add(labels[5]);
		jP.add(labels[16]);
		jP.add(labels[14]);
		jP.add(labels[7]);
		jP.add(labels[15]);
		
		game[0][0] = "src/MapGameImages/1.png";
		game[0][1] = "src/MapGameImages/10.png";
		game[0][2] = "src/MapGameImages/6.png";
		game[0][3] = "src/MapGameImages/3.png";
		game[1][0] = "src/MapGameImages/11.png";
		game[1][1] = "src/MapGameImages/2.png";
		game[1][2] = "src/MapGameImages/12.png";
		game[1][3] = "src/MapGameImages/8.png";
		game[2][0] = "src/MapGameImages/4.png";
		game[2][1] = "src/MapGameImages/13.png";
		game[2][2] = "src/MapGameImages/9.png";
		game[2][3] = "src/MapGameImages/5.png";
		game[3][0] = "src/MapGameImages/16.png";
		game[3][1] = "src/MapGameImages/14.png";
		game[3][2] = "src/MapGameImages/7.png";
		game[3][3] = "src/MapGameImages/15.png";
		
		add(jP, BorderLayout.CENTER);
		add(head, BorderLayout.NORTH);

	}
//	public static void main(String args[]){
//		SwingUtilities.invokeLater(new Runnable(){						/*InvokeLater*/
//			public void run(){	
//				createAndShowGUI();
//			}
//		});
//	}
//	private static void createAndShowGUI(){
//		MapGame frame = new MapGame();
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setSize(new Dimension(640,480));
//		frame.setLocationRelativeTo( null );
//		frame.setResizable(false);
//		frame.setTitle("Map Game");
//		frame.setVisible(true);
//
//	}

	public void mouseClicked(MouseEvent e) {}
	public void mouseEntered(MouseEvent e) {}
	public void mouseExited(MouseEvent e) {}
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == labels[1] || e.getSource() == labels[2] || e.getSource() == labels[3] ||
		   e.getSource() == labels[4] || e.getSource() == labels[5] || e.getSource() == labels[6] ||
		   e.getSource() == labels[7] || e.getSource() == labels[8] || e.getSource() == labels[9] ||                                                                                            
		   e.getSource() == labels[10] || e.getSource() == labels[11] || e.getSource() == labels[12] ||
		   e.getSource() == labels[13] || e.getSource() == labels[14] || e.getSource() == labels[15] ||
		   e.getSource() == labels[16]){
			if(firstClick){
				tempHold[0] = (JLabel) e.getSource();
				firstClick = false;
			}
			else{
				tempHold[1] = (JLabel) e.getSource();
					
				tempImage[0] = (ImageIcon) tempHold[0].getIcon();
				tempImage[1] = (ImageIcon) tempHold[1].getIcon();

				ImageIcon temp = new ImageIcon();
						

			
				int x = 0;
				int y = 0;
				int x2 = 0;
				int y2 = 0;
				
				for(int i = 0; i < 4; i++){
					for(int j = 0; j < 4; j++){
						if(board[i][j].equals(tempHold[0].getIcon().toString())){
							x = i;
							y = j;
						}
						if(board[i][j].equals(tempHold[1].getIcon().toString())){
							x2 = i;
							y2 = j;
						}
					}
				}
				
				game[x][y] = tempHold[1].getIcon().toString();
				game[x2][y2] = tempHold[0].getIcon().toString();
				
				temp = tempImage[1];
				tempImage[1] = tempImage[0];
				tempImage[0] = temp;
				


				for(int i = 0; i < 4; i++){
					for(int j = 0; j < 4; j++){
						if(board[i][j].equalsIgnoreCase(game[i][j])){
							checking[i][j] = 1;
						}
//						else{
//							win = false;
//							//break;
//						}
					}
				}
			for(int i = 0; i < 4; i++){
				for(int j = 0; j < 4; j++){
					if(checking[i][j] == 1){
						win = true;
					}
					else{
						win = false;
						break;
					}
				}
			}
				if(win){
					String message1 = "You Have Solved The Puzzle";
					JOptionPane.showMessageDialog(null, message1,"WINNER", JOptionPane.INFORMATION_MESSAGE);
				}
				
				tempHold[0].removeAll();
				tempHold[1].removeAll();
					
				tempHold[0].setIcon(tempImage[0]);
				tempHold[1].setIcon(tempImage[1]);
				
				tempHold[0] = null;
				tempHold[1] = null;

				tempImage[0] = null;
				tempImage[1] = null;
						
				firstClick = true;
			}
		}
	}
	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	public boolean beenPlayed()
	{
		return played;
	}
	
	public void setPlayed(boolean p)
	{
		played = p;
	}
}
